#include "widget.h"
#include "./ui_widget.h"
#include <QTcpServer>
#include <QString>
#include <QNetworkAccessManager>
#include <QFileDialog>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include  <QNetworkRequest>
#include <QJsonArray>
#include <QDebug>
#include <QTimer>
#include<QDateTimeAxis>

Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget) {
    ui->setupUi(this);
    connectSocket();
    getToken();
    audioInit();
    connect(ui->pushButton_1, &QPushButton::clicked, this, &Widget::sendMessage);
    connect(ui->pushButton_2, &QPushButton::pressed, this, &Widget::Speech);
    connect(ui->pushButton_2, &QPushButton::released, this, &Widget::analyzeSpeech);
    connect(ui->pushButton_7, &QPushButton::clicked, this, [=] { control("0", "1"); });
    connect(ui->pushButton_8, &QPushButton::clicked, this, [=] { control("2", "3"); });
    connect(ui->pushButton_9, &QPushButton::clicked, this, [=] { control("4", "8"); });
    connect(ui->pushButton_10, &QPushButton::clicked, this, [=] { control("9", "10"); });
}



void Widget::connectSocket() {
    camera_socket = new QTcpSocket();
    QString string = "192.168.93.132";
    qDebug()<<string;
    camera_socket->connectToHost(QHostAddress(string), 8080);
    connect(camera_socket, &QTcpSocket::readyRead, this, &Widget::receiveCamera);
    command_socket = new QTcpSocket();
    command_socket->connectToHost(QHostAddress(string), 8081);
    connect(command_socket, &QTcpSocket::readyRead, this, &Widget::getCommand);
    info_socket = new QTcpSocket();
    info_socket->connectToHost(QHostAddress(string), 8082);
    connect(info_socket, &QTcpSocket::readyRead, this, &Widget::getInfo);
}

void Widget::audioInit() {
    audioDeviceInfo = QAudioDeviceInfo::defaultInputDevice();
    QAudioFormat format;
    format.setSampleRate(16000);//设置声音的采样频率
    format.setChannelCount(1);//设置声音的单声道
    format.setSampleSize(16);//设置16位深的编码
    format.setCodec("audio/pcm");//编码器
    format.setSampleType(QAudioFormat::SignedInt);//格式
    format.setByteOrder(QAudioFormat::LittleEndian);//设置高低位
    audioInput = new QAudioInput(audioDeviceInfo, format, this);
}

void Widget::getCommand() {
    QByteArray byteArray = command_socket->readAll();
    qDebug() << QString(byteArray);
}

void Widget::getInfo() {
    QByteArray byteArray = info_socket->readAll();
    qDebug() << byteArray;
    int temperature, humidity, lighting;
    sscanf(byteArray.constData(), "%d %d %d", &temperature, &humidity, &lighting);
    //显示数据
    qDebug() << temperature;
    qDebug() << humidity;
    qDebug() << lighting;
    ui->label_2->setText(QString("温度：%1").arg(temperature));
    ui->label_3->setText(QString("湿度：%1").arg(humidity));
    ui->label_4->setText(QString("光照：%1").arg(lighting));
}


void Widget::receiveCamera() {
    QByteArray byteArray = camera_socket->read(5);
    int len = QString(byteArray).toInt();
    while (camera_socket->bytesAvailable() < len) {
        camera_socket->waitForReadyRead(10);
    }
    QByteArray pic = camera_socket->read(len);
    QPixmap pixmap;
    pixmap.loadFromData(pic);
    ui->label->setPixmap(pixmap);
}


//post请求
void Widget::sendMessage() {
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    qDebug() << "OpenSSL支持情况:" << QSslSocket::supportsSsl();
    qDebug() << "QSslSocket=" << QSslSocket::sslLibraryBuildVersionString();
    QNetworkRequest request;
    QString question = ui->lineEdit_2->text();
    QString url = "https://apis.tianapi.com/robot/index";
    request.setUrl(QUrl(url));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    QByteArray body1;
    body1.append("key=703c91621cc446936cc85b1b6bc7ab85&");
    body1.append(QString("question=%1&").arg(question));
    QNetworkReply *reply = manager->post(request, body1);
    connect(manager, &QNetworkAccessManager::finished, [=]() {
        if (reply->error() == QNetworkReply::NoError) {
            // 请求成功
            qDebug() << "POST请求成功";
            QByteArray temp = reply->readAll();
            QJsonDocument doc = QJsonDocument::fromJson(temp);
            QJsonObject date = doc.object();
            qDebug() << date;
            QJsonObject results = date["result"].toObject();
            QString str = results["reply"].toString();
            if (!str.isEmpty())
                ui->textEdit->setPlainText(str);
            else
                ui->textEdit->setPlainText(date["msg"].toString());
        } else {
            // 请求失败
            qDebug() << "POST请求失败:" << reply->errorString();
        }
        // 释放资源
        reply->deleteLater();
    });
}

void Widget::Speech() {
    // 开始读取音频数据
    buffer = new QBuffer(this);
    buffer->open(QIODevice::ReadWrite);
    audioInput->start(buffer);
    qDebug() << "start...";
}

void Widget::analyzeSpeech() {
    audioInput->stop();
    buffer->close();
    QByteArray byte_array = buffer->data();//存数据
    // 这里可以对音频数据进行处理，这里只是简单输出到控制台
    qDebug() << "Audio data read:" << byte_array.size() << "bytes";

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    // 创建JSON对象并设置数据
    QJsonObject json;
    QString base64 = QString(byte_array.toBase64());
    json["format"] = "pcm";  //string	必填	语音文件的格式，pcm/wav/
    json["rate"] = 16000;//int	必填	采样率，16000、8000，固定值
    json["channel"] = 1;   //int	必填	声道数，仅支持单声道，请填写固定
    json["cuid"] = "dasddas";//string	必填	用户唯一标识，用来区分用户，计算
    json["token"] = access_token; //string	必填	开放平台获取到的开发者[acce
//    json["dev_pid"] = 30;   //int	选填	不填写lan参数生效，都不填写，
//    json["lm_id"] = 30; //int	选填	自训练平台模型id，填dev_pid =
    json["speech"] = QString(byte_array.toBase64()); //string	必填	本地语音文件的二进制语音数据 ，
    json["len"] = byte_array.count();;//int	必填	本地语音文件的的字节数，单位字节
    // 将JSON对象转换为JSON文档
    QJsonDocument jsonData(json);
    QByteArray postData = jsonData.toJson();
    // 创建POST请求
    QNetworkRequest request(QUrl("http://vop.baidu.com/server_api"));
    // 设置请求头，指定内容类型为JSON
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    // 发送POST请求
    QNetworkReply *reply = manager->post(request, postData);
    // 处理响应
    QObject::connect(manager, &QNetworkAccessManager::finished, [=]() {
        if (reply->error() == QNetworkReply::NoError) {
            // 请求成功
            qDebug() << "POST请求成功";
            QByteArray qByteArray = reply->readAll();
            QJsonDocument document = QJsonDocument::fromJson(qByteArray);
            QJsonObject object = document.object();
            qDebug() << object;
            QString string = object["result"].toArray().at(0).toString();
            qDebug() << string;
            QString command = "";
            if (string == "开灯。")command = "0";
            if (string == "关灯。")command = "1";
            if (string == "开蜂鸣器。")command = "3";
            if (string == "关蜂鸣器。")command = "3";
            if (string == "开风扇。")command = "4";
            if (string == "关风扇。")command = "8";
            if (string == "开数码管。")command = "9";
            if (string == "关数码管。")command = "10";
            if (!command.isEmpty())
                command_socket->write(command.toLocal8Bit());
            else
                ui->lineEdit_2->setText(string);
        } else {
            // 请求失败
            qDebug() << "POST请求失败:" << reply->errorString();
        }
        // 释放资源
        reply->deleteLater();
    });
}

void Widget::getToken() {
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    QString client_id = "hGhbLEvR46uFc7zQvbgaFdA6";
    QString client_secret = "2vnKMbRCyVjMaik7CM2JSuofa8SiGSAf";
    QString string = QString(
            "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%1&client_secret=%2").arg(
            client_id).arg(client_secret);
    QNetworkRequest request((QUrl(string)));
    // 设置请求头，指定内容类型为JSON
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    // 发送POST请求
    QNetworkReply *reply = manager->post(request, QByteArray());
    // 处理响应
    connect(manager, &QNetworkAccessManager::finished, [=]() {
        if (reply->error() == QNetworkReply::NoError) {
            // 请求成功
            qDebug() << "POST请求成功";
            QByteArray byteArray = reply->readAll();
            QJsonDocument document = QJsonDocument::fromJson(byteArray);
            QJsonObject date = document.object();
            access_token = date["access_token"].toString();
        } else {
//             请求失败
            qDebug() << "POST请求失败:" << reply->errorString();
        }
        qDebug() << access_token;
        // 释放资源
        reply->deleteLater();
    });
}


void Widget::control(QString open, QString close) {
    QPushButton *pushButton = qobject_cast<QPushButton *>(sender());
    QString string = pushButton->text();
    qDebug() << string;
    if (string.startsWith("开")) {
        string.replace(0, 1, "关");
        pushButton->setText(string);
        command_socket->write(open.toLocal8Bit());
    } else {
        string.replace(0, 1, "开");
        pushButton->setText(string);
        command_socket->write(close.toLocal8Bit());
    }
}



Widget::~Widget() {
    delete ui;
}


